package com.ykdl.member.kid.doctor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.adapters.AskDoctorAdapter;
import com.ykdl.member.kid.adapters.AskDoctorTagAdapter;
import com.ykdl.member.kid.beans.AskDoctorBean;
import com.ykdl.member.kid.beans.AskDoctorBeanListBean;
import com.ykdl.member.kid.beans.DoctorTagBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.gears.FindOtherActivity;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.regandlogin.RegAndLoginActivity;
import com.ykdl.member.kid.util.AccessTokenKeeper;
import com.ykdl.member.kid.util.ScreenUtilsProvider;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.kid.widget.RefreshListView.OnRefreshListener;
import com.ykdl.member.views.MyGallery;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;
/**
 * 问题医生主Acivity
 * @author chenshuai
 */
public class AskDoctorActivity extends BaseScreen{
	private AskDoctorAdapter adapter ;
	private RefreshListView ask_doctor_list ;
	private ArrayList<AskDoctorBean>  list;
	private String TAG = "AskDoctorActivity";
	private MyGallery ask_doctor_tag ;
	private AskDoctorTagAdapter tagadapter ;
	private ImageButton ask_thedoctor ;
	private String doctor_tag = "";
	private AccessTokenKeeper mAccessTokenKeeper ;
	private int start_id = 0;
	private int cursor = 0;
	private int count = 30;
	private int selpos;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.askdoctoractivity_xml);
		mAccessTokenKeeper = new AccessTokenKeeper(this);
		inint();
	}
	public void inint() {
		findViewById(R.id.main_left).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				go(DoctorListAcivity.class);
			}
		});
		findViewById(R.id.main_right).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AskDoctorActivity.this.finish();
			}
		});
		adapter = new AskDoctorAdapter(this,true);
		tagadapter = new AskDoctorTagAdapter(this);
		ask_doctor_list = (RefreshListView)findViewById(R.id.ask_doctor_list);
		ask_doctor_list.setonRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				cursor = 0;
				inintdata();
			}});
		ask_doctor_list.setMoreClick(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				inintdata();
			}});
		ask_doctor_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				AskDoctorBean bean= (AskDoctorBean)adapter.getItem(arg2);
				Intent intent = new Intent(AskDoctorActivity.this,ProblemDetailsAcivity.class);
				intent.putExtra(KidAction.DOCTOR_TOPICI_DD, bean.getTopic_id()+"");
				AskDoctorActivity.this.startActivity(intent);
			}
		});
		ask_doctor_tag = (MyGallery)findViewById(R.id.ask_doctor_tag);
		ask_doctor_tag.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				doctor_tag = tagadapter.getItem(arg2).toString();
				cursor = 0;
				 inintdata();
				tagadapter.setSelectedTab(arg2);
			}
		});
		findViewById(R.id.ask_thedoctor).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (mAccessTokenKeeper.getmisAnonymous()) {
					Intent t= new Intent(AskDoctorActivity.this,RegAndLoginActivity.class);
					t.putExtra( KidAction.FROM, "");
					AskDoctorActivity.this.startActivity(t);
				} else {
					go(ToAskDoctorActivity.class);
				}
			}
		});
		getTag();
	}
	public void getTag() {
		showProgress("正在加载中,请稍等...");
		String  pageurl =KidConfig.GET_DOCTOR_TAG_LIST;
		Map<String,String> map = new HashMap<String, String>();
		HttpBaseRequest request  =  Wxxr.getInstance().getBaseRequest(pageurl);
		 TaskManager.startHttpRequest(request, new getDoctortagTag(),  DoctorTagBean.class);
	}
	
	class getDoctortagTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof DoctorTagBean) {
				DoctorTagBean bean = (DoctorTagBean)t;
				List<String> list = bean.getTags();
				list.add(0, "我的");
				list.add(1, "最新");
				if (list.size()>0) {
					tagadapter.setSelectedTab(0);
				}
				tagadapter.setlist(bean.getTags());
				ask_doctor_tag.setAdapter(tagadapter);
				tagadapter.notifyDataSetChanged();
				doctor_tag = bean.getTags().get(0).toString();
				inintdata();
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	public void inintdata() {
		showProgress("正在加载中,请稍等...");
		String  pageurl;
		pageurl =KidConfig.GET_DOCTOR_QUESTIONS_LIST;
		Map<String,String> map = new HashMap<String, String>();
		if (doctor_tag.equals("我的")) {
			if (mAccessTokenKeeper.getMactor_id()==null) {
				map.put("post_actor_id","0" );
			}else {
				map.put("post_actor_id",mAccessTokenKeeper.getMactor_id() );	
			}
		}else if(doctor_tag.equals("最新")){
			pageurl =KidConfig.GET_DOCTOR_LATEST_QUESTIONS_LIST;
			map.put("answer_status", "2,3");
		}else {
			map.put("tag", doctor_tag);
			map.put("answer_status", "2,3");
		}
		map.put("start_id", String.valueOf(start_id));
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		HttpBaseRequest request  =  Wxxr.getInstance().getRequest(pageurl, map,"");
		 TaskManager.startHttpRequest(request, new getDoctorQuestionsListTag(),  AskDoctorBeanListBean.class);
	}
	
	class getDoctorQuestionsListTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof AskDoctorBeanListBean) {
				AskDoctorBeanListBean bean = (AskDoctorBeanListBean)t;
				list = bean.getList();
				if (list!=null) {
					hidErrorTip( );
					adapter.setList(bean.getList());
					ask_doctor_list.setVisibility(View.VISIBLE);
					ask_doctor_list.setAdapter(adapter);
					adapter.notifyDataSetChanged();
					if (bean.getNext_cursor() == bean.getTotal_number()) {
						ask_doctor_list.setMoreButtoIsGon(true);
					} else {
						ask_doctor_list.setMoreButtoIsGon(false);
						cursor = bean.getNext_cursor();
					}
				}else {
					ask_doctor_list.setVisibility(View.GONE);
					showErrorTip("没有数据...", "");
					ask_doctor_list.setMoreButtoIsGon(true);
				}
				
				ask_doctor_list.onRefreshComplete();
				ask_doctor_list.setFecthMoreOk();
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		getTag();
		
		ImageLoader.getInstance().setCurrentActivity(TAG);
		super.onResume();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().pause(TAG);
		super.onPause();
	}
}
